home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / C and C++ / Gnuplot 3.5 for Macintosh / SOURCES 3.5 / scanner.c < prev    next >
Text File  |  1993-11-12  |  11KB  |  410 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: scanner.c%v 3.50 1993/07/09 05:35:24 woo Exp $";
  3. #endif
  4.  
  5.  
  6. /* GNUPLOT - scanner.c */
  7. /*
  8.  * Copyright (C) 1986 - 1993   Thomas Williams, Colin Kelley
  9.  *
  10.  * Permission to use, copy, and distribute this software and its
  11.  * documentation for any purpose with or without fee is hereby granted, 
  12.  * provided that the above copyright notice appear in all copies and 
  13.  * that both that copyright notice and this permission notice appear 
  14.  * in supporting documentation.
  15.  *
  16.  * Permission to modify the software is granted, but not the right to
  17.  * distribute the modified code.  Modifications are to be distributed 
  18.  * as patches to released version.
  19.  *  
  20.  * This software is provided "as is" without express or implied warranty.
  21.  * 
  22.  *
  23.  * AUTHORS
  24.  * 
  25.  *   Original Software:
  26.  *     Thomas Williams,  Colin Kelley.
  27.  * 
  28.  *   Gnuplot 2.0 additions:
  29.  *       Russell Lang, Dave Kotz, John Campbell.
  30.  *
  31.  *   Gnuplot 3.0 additions:
  32.  *       Gershon Elber and many others.
  33.  * 
  34.  * There is a mailing list for gnuplot users. Note, however, that the
  35.  * newsgroup 
  36.  *    comp.graphics.gnuplot 
  37.  * is identical to the mailing list (they
  38.  * both carry the same set of messages). We prefer that you read the
  39.  * messages through that newsgroup, to subscribing to the mailing list.
  40.  * (If you can read that newsgroup, and are already on the mailing list,
  41.  * please send a message info-gnuplot-request@dartmouth.edu, asking to be
  42.  * removed from the mailing list.)
  43.  *
  44.  * The address for mailing to list members is
  45.  *       info-gnuplot@dartmouth.edu
  46.  * and for mailing administrative requests is 
  47.  *       info-gnuplot-request@dartmouth.edu
  48.  * The mailing list for bug reports is 
  49.  *       bug-gnuplot@dartmouth.edu
  50.  * The list of those interested in beta-test versions is
  51.  *       info-gnuplot-beta@dartmouth.edu
  52.  */
  53.  
  54. #include <stdio.h>
  55. #include <ctype.h>
  56. #include "plot.h"
  57. #ifdef THINK_C
  58. #include "tout_protos.h"
  59. #endif
  60.  
  61. #ifdef AMIGA_AC_5
  62. #define O_RDONLY    0
  63. int open(const char * _name, int _mode, ...);
  64. int close(int);
  65. #endif
  66.  
  67. #ifdef vms
  68.  
  69. #include stdio
  70. #include descrip
  71. #include errno
  72.  
  73. #define MAILBOX "PLOT$MAILBOX"
  74. #define pclose(f) fclose(f)
  75.  
  76. #endif /* vms */
  77.  
  78.  
  79. #define isident(c) (isalnum(c) || (c) == '_')
  80.  
  81. #ifndef STDOUT
  82. #define STDOUT 1
  83. #endif
  84.  
  85. #define LBRACE '{'
  86. #define RBRACE '}'
  87.  
  88. #define APPEND_TOKEN {token[t_num].length++; current++;}
  89.  
  90. #define SCAN_IDENTIFIER while (isident(expression[current + 1]))\
  91.                 APPEND_TOKEN
  92.  
  93. extern struct lexical_unit token[MAX_TOKENS];
  94.  
  95. static int t_num;    /* number of token I'm working on */
  96.  
  97. #ifndef AMIGA_SC_6_1
  98. char *strcat(), *strcpy(), *strncpy();
  99. #endif /* !AMIGA_SC_6_1 */
  100.  
  101. /*
  102.  * scanner() breaks expression[] into lexical units, storing them in token[].
  103.  *   The total number of tokens found is returned as the function value.
  104.  *   Scanning will stop when '\0' is found in expression[], or when token[]
  105.  *     is full.
  106.  *
  107.  *     Scanning is performed by following rules:
  108.  *
  109.  *    Current char    token should contain
  110.  *     -------------    -----------------------
  111.  *    1.  alpha,_    all following alpha-numerics
  112.  *    2.  digit    0 or more following digits, 0 or 1 decimal point,
  113.  *                0 or more digits, 0 or 1 'e' or 'E',
  114.  *                0 or more digits.
  115.  *    3.  ^,+,-,/    only current char
  116.  *        %,~,(,)
  117.  *        [,],;,:,
  118.  *        ?,comma
  119.  *    4.  &,|,=,*    current char; also next if next is same
  120.  *    5.  !,<,>    current char; also next if next is =
  121.  *    6.  ", '    all chars up until matching quote
  122.  *    7.  #        this token cuts off scanning of the line (DFK).
  123.  *
  124.  *            white space between tokens is ignored
  125.  */
  126. scanner(expression)
  127. char expression[];
  128. {
  129. register int current;    /* index of current char in expression[] */
  130. register int quote;
  131. char brace;
  132.  
  133.     for (current = t_num = 0;
  134.         t_num < MAX_TOKENS && expression[current] != '\0';
  135.         current++) {
  136. again:
  137.         if (isspace(expression[current]))
  138.             continue;                        /* skip the whitespace */
  139.         token[t_num].start_index = current;
  140.         token[t_num].length = 1;
  141.         token[t_num].is_token = TRUE;    /* to start with...*/
  142.  
  143.         if (expression[current] == '`') {
  144.             substitute(&expression[current],MAX_LINE_LEN - current);
  145.             goto again;
  146.         }
  147.         /* allow _ to be the first character of an identifier */
  148.         if (isalpha(expression[current]) || expression[current] == '_') {
  149.             SCAN_IDENTIFIER;
  150.         } else if (isdigit(expression[current]) || expression[current] == '.'){
  151.             token[t_num].is_token = FALSE;
  152.             token[t_num].length = get_num(&expression[current]);
  153.             current += (token[t_num].length - 1);
  154.         } else if (expression[current] == LBRACE) {
  155.             token[t_num].is_token = FALSE;
  156.             token[t_num].l_val.type = CMPLX;
  157. #ifdef __PUREC__
  158.             { char    l[80];
  159.             if ((sscanf(&expression[++current],"%lf,%lf%[ }]s",
  160.                 &token[t_num].l_val.v.cmplx_val.real,
  161.                 &token[t_num].l_val.v.cmplx_val.imag,
  162.                 &l)    != 3) || (!strchr(l, RBRACE))  )
  163.                     int_error("invalid complex constant",t_num);
  164.             }
  165. #else
  166.             if ((sscanf(&expression[++current],"%lf , %lf %c",
  167.                 &token[t_num].l_val.v.cmplx_val.real,
  168.                 &token[t_num].l_val.v.cmplx_val.imag,
  169.                 &brace) != 3) || (brace != RBRACE))
  170.                     int_error("invalid complex constant",t_num);
  171. #endif
  172.             token[t_num].length += 2;
  173.             while (expression[++current] != RBRACE) {
  174.                 token[t_num].length++;
  175.                 if (expression[current] == '\0')            /* { for vi % */
  176.                     int_error("no matching '}'", t_num);
  177.             }
  178.         } else if (expression[current] == '\'' || expression[current] == '\"'){
  179.             token[t_num].length++;
  180.             quote = expression[current];
  181.             while (expression[++current] != quote) {
  182.                 if (!expression[current]) {
  183.                     expression[current] = quote;
  184.                     expression[current+1] = '\0';
  185.                     break;
  186.                 } else
  187.                     token[t_num].length++;
  188.             }
  189.         } else switch (expression[current]) {
  190.              case '#':        /* DFK: add comments to gnuplot */
  191.                   goto endline; /* ignore the rest of the line */
  192.             case '^':
  193.             case '+':
  194.             case '-':
  195.             case '/':
  196.             case '%':
  197.             case '~':
  198.             case '(':
  199.             case ')':
  200.             case '[':
  201.             case ']':
  202.             case ';':
  203.             case ':':
  204.             case '?':
  205.             case ',':
  206.                 break;
  207.             case '&':
  208.             case '|':
  209.             case '=':
  210.             case '*':
  211.                 if (expression[current] == expression[current + 1])
  212.                     APPEND_TOKEN;
  213.                 break;
  214.             case '!':
  215.             case '<':
  216.             case '>':
  217.                 if (expression[current + 1] == '=')
  218.                     APPEND_TOKEN;
  219.                 break;
  220.             default:
  221.                 int_error("invalid character",t_num);
  222.             }
  223.         ++t_num;    /* next token if not white space */
  224.     }
  225.  
  226. endline:                    /* comments jump here to ignore line */
  227.  
  228. /* Now kludge an extra token which points to '\0' at end of expression[].
  229.    This is useful so printerror() looks nice even if we've fallen off the
  230.    line. */
  231.  
  232.         token[t_num].start_index = current;
  233.         token[t_num].length = 0;
  234.     return(t_num);
  235. }
  236.  
  237.  
  238. get_num(str)
  239. char str[];
  240. {
  241. double atof();
  242. register int count = 0;
  243. long atol();
  244. register long lval;
  245.  
  246.     token[t_num].is_token = FALSE;
  247.     token[t_num].l_val.type = INTGR;        /* assume unless . or E found */
  248.     while (isdigit(str[count]))
  249.         count++;
  250.     if (str[count] == '.') {
  251.         token[t_num].l_val.type = CMPLX;
  252.         while (isdigit(str[++count]))    /* swallow up digits until non-digit */
  253.             ;
  254.         /* now str[count] is other than a digit */
  255.     }
  256.     if (str[count] == 'e' || str[count] == 'E') {
  257.         token[t_num].l_val.type = CMPLX;
  258. /* modified if statement to allow + sign in exponent
  259.    rjl 26 July 1988 */
  260.         count++;
  261.         if (str[count] == '-' || str[count] == '+')
  262.             count++;
  263.         if (!isdigit(str[count])) {
  264.             token[t_num].start_index += count;
  265.             int_error("expecting exponent",t_num);
  266.         }
  267.         while (isdigit(str[++count]))
  268.             ;
  269.     }
  270.     if (token[t_num].l_val.type == INTGR) {
  271.          lval = atol(str);
  272.         if ((token[t_num].l_val.v.int_val = lval) != lval)
  273.             int_error("integer overflow; change to floating point",t_num);
  274.     } else {
  275.         token[t_num].l_val.v.cmplx_val.imag = 0.0;
  276.         token[t_num].l_val.v.cmplx_val.real = atof(str);
  277.     }
  278.     return(count);
  279. }
  280.  
  281. #if defined(unix) || defined(vms) || defined(PIPES) || (defined(ATARI) && defined(__PUREC__))
  282.  
  283. substitute(str,max)            /* substitute output from ` ` */
  284. char *str;
  285. int max;
  286. {
  287. register char *last;
  288. register int i,c;
  289. register FILE *f;
  290. #ifdef AMIGA_AC_5
  291. int fd;
  292. #else
  293. #if defined(ATARI) && defined(__PUREC__)
  294. char    *atari_tmpfile;
  295. char    *atari_pgm[MAX_LINE_LEN+100];
  296. #else
  297. FILE *popen();
  298. #endif /* ATARI && PUREC */
  299. #endif
  300. static char pgm[MAX_LINE_LEN+1],output[MAX_LINE_LEN+1];
  301.  
  302. #ifdef vms
  303. int chan;
  304. static $DESCRIPTOR(pgmdsc,pgm);
  305. static $DESCRIPTOR(lognamedsc,MAILBOX);
  306. #endif /* vms */
  307.  
  308.     i = 0;
  309.     last = str;
  310.     while (*(++last) != '`') {
  311.         if (*last == '\0')
  312.             int_error("unmatched `",t_num);
  313.         pgm[i++] = *last;
  314.     }
  315.     pgm[i] = '\0';        /* end with null */
  316.     max -= strlen(last);    /* max is now the max length of output sub. */
  317.   
  318. #ifdef vms
  319.       pgmdsc.dsc$w_length = i;
  320.        if (!((vaxc$errno = sys$crembx(0,&chan,0,0,0,0,&lognamedsc)) & 1))
  321.            os_error("sys$crembx failed",NO_CARET);
  322.    
  323.        if (!((vaxc$errno = lib$spawn(&pgmdsc,0,&lognamedsc,&1)) & 1))
  324.            os_error("lib$spawn failed",NO_CARET);
  325.    
  326.        if ((f = fopen(MAILBOX,"r")) == NULL)
  327.            os_error("mailbox open failed",NO_CARET);
  328. #else /* vms */
  329. #if defined(ATARI) && defined(__PUREC__)
  330.         if (system(NULL) == 0)
  331.             os_error("no command shell");
  332.         if ((strlen(atari_tmpfile) + strlen(pgm) + 5) > MAX_LINE_LEN+100)
  333.             os_error("sorry, command to long");
  334.         atari_tmpfile = tmpnam(NULL);
  335.         strcpy(atari_pgm, pgm);
  336.         strcat(atari_pgm, " >> ");
  337.         strcat(atari_pgm, atari_tmpfile);
  338.         system(atari_pgm);
  339.         if ((f = fopen(atari_tmpfile, "r")) == NULL)
  340. #else
  341. #ifdef AMIGA_AC_5
  342.       if ((fd = open(pgm,"O_RDONLY")) == -1)
  343. #else
  344.       if ((f = popen(pgm,"r")) == NULL)
  345. #endif
  346. #endif    /* ATARI && PUREC */
  347.           os_error("popen failed",NO_CARET);
  348. #endif /* vms */
  349.  
  350.     i = 0;
  351.     while ((c = getc(f)) != EOF) {
  352.         output[i++] = ((c == '\n') ? ' ' : c);    /* newlines become blanks*/
  353.         if (i == max) {
  354. #ifdef AMIGA_AC_5
  355.             (void) close(fd);
  356. #else
  357. #if defined(ATARI) && defined(__PUREC__)
  358.             (void) fclose(f);
  359.             (void) unlink(atari_tmpfile);
  360. #else
  361.             (void) pclose(f);
  362. #endif /* ATARI && PUREC */
  363. #endif
  364.             int_error("substitution overflow", t_num);
  365.         }
  366.     }
  367. #ifdef AMIGA_AC_5
  368.     (void) close(fd);
  369. #else
  370. #if defined(ATARI) && defined(__PUREC__)
  371.     (void) fclose(f);
  372.     (void) unlink(atari_tmpfile);
  373. #else
  374.     (void) pclose(f);
  375. #endif /* ATARI && PUREC */
  376. #endif
  377.  
  378.     if (i + strlen(last) > max)
  379.         int_error("substitution overflowed rest of line", t_num);
  380.     (void) strncpy(output+i,last+1,MAX_LINE_LEN-i);
  381.                                     /* tack on rest of line to output */
  382.     (void) strcpy(str,output);                /* now replace ` ` with output */
  383.     screen_ok = FALSE;
  384. }
  385.  
  386. #else /* unix || vms || PIPES || ATARI && PUREC */
  387.  
  388. #ifdef THINK_C
  389. substitute(char *str,int max)
  390. {
  391.     int_error("substitution not supported by MacOS!",t_num);
  392. }
  393. #elif __ZTC__
  394. substitute(char *str,int max)
  395. {
  396.     char line[100];
  397.  
  398.     int_error( strcat(strcpy(line,"substitution not supported by "),OS),t_num);
  399. }
  400. #else
  401. substitute()
  402. {
  403.     char line[100];
  404.  
  405.     int_error( strcat(strcpy(line,"substitution not supported by "),OS),t_num);
  406. }
  407. #endif
  408.  
  409. #endif /* unix || vms || PIPES || ATARI && PUREC */
  410.